package com.fund.datacopy.task.thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import com.fund.datacopy.bean.Atom;
import com.fund.datacopy.bean.Coper;
import com.fund.datacopy.bean.DocParser;
import com.fund.datacopy.conf.Constants;

public class Allocater implements Runnable {
	private ExecutorService executor = Executors.newFixedThreadPool(Constants.poolSize);
	private DocParser doc;
	private List<Coper> copers = new ArrayList<Coper>();

	public Allocater() {
	}

	public Allocater(DocParser doc) {
		this.doc = doc;
	}

	public DocParser docetDoc() {
		return this.doc;
	}

	public void setDoc(DocParser doc) {
		this.doc = doc;
	}

	void parserToGrouper() {

		String src = doc.getSrc();
		String dest = doc.getDest();
		String sdb = doc.getSdb();
		String ddb = doc.getDdb();
		List<String> stab = doc.getStable();
		List<String> dtab = doc.getDtable();
		List<String> scoll = doc.getScollum();
		List<String> dcoll = doc.getDcollum();
		List<String> style = doc.getStyle();
		List<String> ext = doc.getExt();

		if (StringUtils.isEmpty(sdb) && StringUtils.isEmpty(ddb)) {
			System.err.println("db 未配置..");
			return;
		}
		if (CollectionUtils.isEmpty(stab) && CollectionUtils.isEmpty(dtab)) {
			System.err.println("table 未配置..");
			return;
		}
		if (StringUtils.isBlank(src)) {
			src = Constants.MGT_DB_URL;
		}
		if (StringUtils.isBlank(dest)) {
			src = Constants.DEV_DB_URL;
		}

		if (StringUtils.isBlank(sdb)) {
			sdb = ddb;
		}
		if (StringUtils.isBlank(ddb)) {
			ddb = sdb;
		}

		if (CollectionUtils.isEmpty(stab)) {
			stab = dtab;
		}
		if (CollectionUtils.isEmpty(dtab)) {
			dtab = stab;
		}

		if (CollectionUtils.isEmpty(scoll)) {
			scoll = dcoll;
		}
		if (CollectionUtils.isEmpty(dcoll)) {
			dcoll = scoll;
		}

		if (CollectionUtils.isEmpty(scoll)) {
			scoll = dcoll;
		}
		if (CollectionUtils.isEmpty(dcoll)) {
			dcoll = scoll;
		}

		for (int i = 0; i < stab.size(); i++) {
			Coper coper = new Coper();
			Atom scp = new Atom();
			Atom dcp = new Atom();

			scp.setUrl(src);
			scp.setDb(sdb);
			scp.setTable(stab.get(i));

			if (CollectionUtils.isNotEmpty(scoll) && scoll.size() > i) {
				scp.setCollum(scoll.get(i));
			}

			dcp.setUrl(src);
			dcp.setDb(sdb);
			dcp.setTable(dtab.get(i));

			if (CollectionUtils.isNotEmpty(dcoll) && dcoll.size() > i) {
				dcp.setCollum(dcoll.get(i));
			}

			if (CollectionUtils.isNotEmpty(style) && style.size() > i) {
				scp.setStyle(style.get(i));
				dcp.setStyle(style.get(i));
			}

			if (CollectionUtils.isNotEmpty(ext) && ext.size() > i) {
				scp.setExt(ext.get(i));
				dcp.setExt(ext.get(i));
			}
			coper.setSrc(scp);
			coper.setDest(dcp);
			copers.add(coper);
		}

	}

	public void work() throws Exception {

		if ((this.doc == null)) {
			System.err.println("-----配置不对----");
			return;
		}
		parserToGrouper();
		if (CollectionUtils.isEmpty(copers)) {

		}

		for (int i = 0; i < copers.size(); i++) {
			this.executor.execute(copers.get(i));
		}
		this.executor.shutdown();
	}

	public void run() {
		try {
			System.out.println("alloct resource .." + this.doc.getName());
			work();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
